package com.edu.controller.user;

import com.edu.entity.User;
import com.edu.service.user.UserService;
import com.edu.service.user.UserServiceImpl;
import com.edu.utils.ParamFormat;
import com.edu.utils.ResponseBody;
import com.edu.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author ZCN
 * @Date 2021/11/20 21:11
 * @Version 1.0
 **/
@WebServlet("/user/register")
public class RegisterServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            ParamFormat.format(req, resp);
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            // 参数校验
            StringUtil.isNull(username);
            StringUtil.isNull(password);
            // 表单信息封装user
            User user = new User(username, password);

            String register = userService.register(user);
            ResponseBody.success(resp,register,0L,null);
        } catch (Exception e) {
            e.printStackTrace();
            ResponseBody.failure(resp,"failure");
        }
    }
}
